package com.googlecode.imanager.jasper;

import net.sf.jasperreports.engine.JRDefaultStyleProvider;

import com.googlecode.imanager.client.Container;
import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.Manager;
import com.googlecode.imanager.client.bridge.IContainer;
import com.googlecode.imanager.client.bridge.IControl;
import com.googlecode.imanager.client.bridge.IPanel;
import com.googlecode.imanager.core.base.BridgeApplication;
import com.googlecode.imanager.jasper.print.RFrame;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class RContainer extends RControl implements IContainer, IPanel {

    public RContainer(BridgeApplication bridge, Container source) {
        super(bridge, source);
        //getTarget().getLineBox().getPen().setLineWidth(1);
    }

    @Override
    protected RFrame createTarget() {
        return new RFrame(this);
    }

    @Override
    public RFrame getTarget() {
        return (RFrame) target;
    }

    @Override
    public Container getSource() {
        return (Container) source;
    }

    @Override
    public void setDefaultStyleProvider(JRDefaultStyleProvider defaultStyleProvider) {
        getTarget().setDefaultStyleProvider(defaultStyleProvider);
    }

    @Override
    public void setBounds(int left, int top, int width, int height) {
        super.setBounds(
                left + source.getInsetLeft(), 
                top + source.getInsetTop(), 
                width - source.getInsetLeft() - source.getInsetRight(), 
                height - source.getInsetTop() - source.getInsetBottom());
    }

    public void validateInsets() {
        // TODO calcular bordas
        getManager().setInsets(getSource(), 0, 0, 0, 0);
        setBounds(source.getLeft(), source.getTop(), source.getWidth(), source.getHeight());
    }
    
    public void setTreePrinting(boolean printing) {
        if (printing){
            validateInsets();
        }
        
        for (Control control : getSource().getChildren()){
            IControl impl = Manager.getImpl(control);
            
            if (impl instanceof RControl){
                RControl o = (RControl) impl;

                if (o instanceof RContainer){
                    RContainer c = (RContainer) o;
                    c.setTreePrinting(printing);
                } else {
                    o.setPrinting(printing);
                }
            }
        }

        setPrinting(printing);
    }

}
